<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>JSONP</h1>
    <button id="btn">提交</button>

    <script>

        const oBtn = document.getElementById('btn');
        let oScript = null;
         // 声明一个函数，通过地址获取数据
         function data(data){
                console.log(data);
            }

        oBtn.onclick = function(){

            if(oScript){
                oScript.remove()
            }

            //利用script 没有跨域的特性 再给他添加src的属性 
            oScript = document.createElement('script')
            oScript.src = 'http://127.0.0.1:3000/jsonAll?userId=001&callback=data'
            //添加到元素上
            document.body.appendChild(oScript)
        }

    </script>
</body>
</html>